package com.jgsu.farm.mapper.sql;

import com.jgsu.farm.entity.MatchImage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface MatchImageMapperSql {
    /**
     * 实例对象中主键的属性名keyProperty,数据库中的字段名keyColumn
     * */
    @Insert("insert into match_image(imageID,matchinfo) values(#{imageid},#{matchinfo})")
    @Options(useGeneratedKeys = true, keyColumn = "matchID", keyProperty = "matchid")
    int insert(MatchImage matchImage);

    @Insert("insert into match_image(matchID,imageid,matchinfo) values(#{matchid},#{imageid},#{matchinfo})")
    int insertWithPrimaryKey(MatchImage matchImage);

    @Delete("delete from match_image where matchID = #{matchid}")
    void delete(Integer matchid);

    @Update("update match_image set imageFile = #{imagefile} where matchID = #{matchid}")
    void update(MatchImage matchImage);

    @Select("select imageID from match_image where matchID = #{matchid}")
    List<Integer> select(Integer matchid);


    /*----------------------------------------------------------------------------------*/
    @Select("select matchID,imageID,matchInfo from match_image where matchID = #{matchid}")
    List<MatchImage> selectByMatchId(Integer matchid);

    /**
     * 根据imageid查数据库中存在的数据
     * */
    @Select("select matchID,imageID,matchInfo from match_image where imageid = #{imageid}")
    MatchImage selectByImageId(Integer imageid);

    /**
     * 根据matchid和imageid删除数据
     * */
    @Delete("delete from match_image where matchid = #{matchid} and imageid = #{imageid}")
    void deleteByMatchIdAndImageId(Integer matchid,Integer imageid);

    /**
     * 根据plantid查询数据
     * */
    @Results(id = "matchMap",value = {
            @Result(id = true,column = "matchid",property = "matchid"),
            @Result(column = "imageid",property = "imageid"),
            @Result(column = "imageid",property = "image",one = @One(select = "com.jgsu.farm.mapper.sql.ImageMapperSql.select"))
    })
    @Select("select * from match_image where matchid = #{plantimage}")
    List<MatchImage> findMatchImageByPlantId(Integer plantimage);

}
